<template>
  <div class="payment">
    <div class="address">
      <x-input title="收货人" v-model="name" text-align='right' placeholder="请填写收货人姓名"></x-input>
      <x-input title="手机号码" v-model="phone" type="number" text-align='right' placeholder="请填写收货人手机号码"></x-input>
      <x-address title="省/市/区" v-model="province" @on-shadow-change="onShadowChange" :list="ChinaAddressV4Data" :placeholder="changeProvince"></x-address>
      <x-input class="border-none" title="详细地址" v-model="address" text-align='right' placeholder="请输入街道，门牌等详细地址信息"></x-input>
    </div>
    <div class="goods" v-show="confirmCon.is_from_cart == '0'">
      <div class="img">
        <img :src="goodsLists.img">
      </div>
      <div class="title">
        <p class="tip">{{goodsLists.title}}</p>
        <p class="attr">规格：{{confirmCon.guige}}</p>
        <p class="price">¥{{goodsLists.price}} <span class="num">x{{confirmCon.num}}</span></p>
      </div>
    </div>
    <div class="goods" v-show="confirmCon.is_from_cart == '1'" v-for="(item, index) in goods" :key="index">
      <div class="img">
        <img :src="item.img">
      </div>
      <div class="title">
        <p class="tip">{{item.title}}</p>
        <p class="attr">规格：{{item.guige}}</p>
        <p class="price">¥{{item.price}} <span class="num">x{{item.num}}</span></p>
      </div>
    </div>    
    <div class="goods-all border-none">
      <span class="all-title">商品合计</span>
      <span class="all-price">￥{{allPrice}}</span>
    </div>
    <div class="fixed">
      <div class="left">
        <span class="fl">合计 <em>¥{{allPrice}}</em></span>
      </div>
      <div class="right" @click="toPay"><img src="@/assets/weixinzhifu.png" alt=""> 微信支付</div>
    </div>
  </div>
</template>

<script>
import { XInput, XAddress, ChinaAddressV4Data, AlertModule } from 'vux'
import { wexinPay } from '@/config/wxpay'
import { getGoodsDetails, submitOrder, setAddress, getAddress, getProLists_forCart } from '@/api/api'
export default {
  components: {
    XInput,
    XAddress,
    ChinaAddressV4Data,
    AlertModule,
  },
  data () {
    return {
      name: '',
      phone: '',
      address: '',
      province: [],
      changeProvince: '北京市 市辖区 东城区',
      ChinaAddressV4Data: ChinaAddressV4Data,
      goodsLists: [],
      id:0,
      title:'',
      img:'',
      content:'',
      price:0,
      allPrice:0,
      orderPrice:0,
      picture:0,
      freight:0,
      confirmCon: {},
      goods: [],
    }
  },
  created () {
    this.confirmCon = JSON.parse(this.$route.query.confirmCon);
    if(this.confirmCon.is_from_cart == '0'){
      this.getGoodsList()
    }else{
      this.getProListsForCart();
    }
    this.get_address()
  },
  methods: {    
    router (path) {
      this.$router.push(path)
    },
    getGoodsList() {  //商品详情
      getGoodsDetails(this.confirmCon.ids).then((res) => {
        this.goodsLists = res.data.info
        this.allPrice = this.goodsLists.price * this.confirmCon.num
      })
    },
    toPay(){//去支付
      var self = this;
      let jsonpost = {
        name: this.name,
        phone: this.phone,
        prov_city_area: this.changeProvince,
        address: this.address,
        uid: this.confirmCon.uid,
        ids: this.confirmCon.ids,
        is_from_cart: this.confirmCon.is_from_cart,
        num: this.confirmCon.num,
        guige_id: this.confirmCon.guige_id,
      }
      submitOrder(jsonpost).then((res) => {
        if(res.data.status != 1){
          AlertModule.show({
            title: '系统提示',
            content: '支付失败',
            onHide: () =>{}
          })
        }else{
          wexinPay(res.data.info.jsapi, (sucRes) => {
            this.set_address()
            this.router({path: './paySuccess', query: {order_id: res.data.info.order.id}});
          }, (err) => {
            console.dir(err)
          })
        }
      })

    },
    onShadowChange(ids, val) {  //选择省市区触发
      var provincial_urban_area = '';
      for(var i=0;i<val.length;i++){
        provincial_urban_area += val[i]+' ';
      }
      this.changeProvince = provincial_urban_area;
    },
    set_address() {
      var data = {
        name: this.name,
        phone: this.phone,
        prov_city_area: this.changeProvince,
        address: this.address,
        uid: this.confirmCon.uid,
      }
      setAddress(data).then((res) => {
      }).catch((err) => {
        console.dir(err)
      })
    },
    get_address() {
      getAddress(this.confirmCon.uid).then((res) => {
        this.name = res.data.info.name;
        this.phone = res.data.info.phone;
        this.changeProvince = res.data.info.prov_city_area;
        this.address = res.data.info.address;
      }).catch((err) => {
        console.dir(err)
      })
    },
    getProListsForCart() {
      getProLists_forCart(this.confirmCon.ids, this.confirmCon.uid).then((res) => {
        this.goods = res.data.info;
        for(var i in this.goods){
          this.allPrice = this.goods[i].price * this.goods[i].num + this.allPrice
        }
      }).catch((err) => {
        console.dir(err)
      })
    }
  }

}
</script>
<style lang="stylus" scoped>
.payment
  background-color #f2f2f2
  min-width 100vw
  min-height 100vh
  .address
    margin-bottom 1rem
    background-color #fff
    .weui-cell
      padding 0
      span
        font-size 1.5rem
        color #666
      .vux-cell-placeholder
        color #ccc
    .border-none
      border none !important
    .vux-x-input,.vux-cell-box
      padding 0
      line-height 5rem
      height 5rem
      overflow hidden
      margin 0 1.5rem
      border-bottom 1px solid #e5e5e5
      box-sizing border-box
      &::before
        display none
      label
        font-size 1.5rem
        color #666
      input
        font-size 1.5rem
        color #666
        height 5rem
        line-height 5rem
        &::-webkit-input-placeholder
          color #ccc
  .goods
    display flex
    background-color #fff
    margin-bottom 1rem
    padding 2rem 1.5rem
    .img
      width 12rem
      height 12rem
      margin-right 1.4rem
      img
        height 12rem
        object-fit cover
    .title
      position relative
      .tip
        font-size 1.5rem
        color #333
        width calc(100vw - 17rem)
        overflow hidden
        text-overflow ellipsis
        display -webkit-box
        -webkit-line-clamp 2
        -webkit-box-orient vertical
      .attr
        margin-top 1rem
        color #999
        font-size 1.2rem
      .price
        color #666
        font-size 1.7rem
        line-height 2.4rem
        position absolute
        bottom 0
        font-weight bold
        overflow hidden
        width 100%
        span
          float right
          line-height 2.4rem
    .num
      flex 1
      text-align right
      line-height 2.1rem
      font-size 1.5rem
      font-weight 500
  .goods-all
    background-color #fff
    line-height 5rem
    height 5rem
    display flex
    font-size 1.5rem
    padding 0 1.5rem
    border-bottom 1px solid #e5e5e5
    .all-title
      flex 1
      text-align left
      color #666
    .all-price
      flex 1
      text-align right
      color #333
      font-weight 500
      position relative
      img       
        position absolute
        width 2.2rem
        top 0
        right 6.9rem
        top 1.5rem
  .border-none
    border none
  .fixed
    position fixed
    bottom 0
    height 7rem
    line-height 7rem
    display flex
    width 100%
    background linear-gradient(0deg,rgba(255,255,255,1) 0%)
    padding 1rem
    box-sizing border-box
    .left
      flex 1
      font-size 1.5rem
      color #fff
      line-height 5rem
      height 5rem
      border-radius .4rem 0 0 .4rem
      background-color #171C26
      .fl
        margin-left 1.5rem
      em
        font-size 1.8rem
        font-weight bold
        line-height 5rem
        height 5rem
        display inline-block
    .right
      max-width 15rem
      min-width 15rem
      background-color #43A047
      color #fff
      font-size 1.8rem
      font-weight bold
      text-align center
      line-height 5rem
      border-radius 0 .4rem .4rem 0
      img
        display inline-block
        vertical-align middle
        position relative
        top -.1rem
        width 2.4rem
</style>